package top.chendawei.main.facade;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.chendawei.aop.auth.CustomsSecurityProperties;
import top.chendawei.system.beans.entity.SysUser;
import top.chendawei.util.fastjson.FastJsonUtil;
import top.chendawei.util.http.CookieUtil;
import top.chendawei.util.http.HttpServletUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class UserCookieFacadeImpl {
    @Autowired
    private CustomsSecurityProperties customsSecurityProperties;

    @Autowired
    private UserFacadeImpl userFacade;


    public static final Cache<String, SysUser> USER_CACHE =
            CacheBuilder.newBuilder().maximumSize(1000)
                    .expireAfterAccess(30L, TimeUnit.MINUTES).build();

    public SysUser getCookieUser(String sessionId) {
        SysUser u = USER_CACHE.getIfPresent(sessionId);
        if (null != u) {
            return u;
        }
        String token = CookieUtil.getCookieValueByName(HttpServletUtil.getHttpRequest(), UserFacadeImpl.CUR_USER);
        if (StringUtils.isBlank(token)) {
            log.info("getCookieUser null 1");
            return null;
        }
        String userStr = userFacade.getDecode(token);
        if (StringUtils.isBlank(userStr)) {
            log.info("getCookieUser error 2");
            return null;
        }
        u = FastJsonUtil.parse(userStr, SysUser.class);
        Long t1 = System.currentTimeMillis() - u.getT();
        if (t1 > customsSecurityProperties.getSessionOutTime()) {
            return null;
        }
        saveUser2Cookie(u, t1);
        USER_CACHE.put(sessionId, u);
        return u;
    }

    public void rmUser(String sessionId) {
        USER_CACHE.invalidate(sessionId);
    }


    public void saveUser2Cookie(SysUser u, Long t1) {
        if (customsSecurityProperties.isUseCokkie() && t1 > customsSecurityProperties.getSessionRefreshTime()) {
            u.setT(System.currentTimeMillis());

            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletResponse response = requestAttributes.getResponse();
            u.setT(System.currentTimeMillis());
            CookieUtil.setCookie(response, UserFacadeImpl.CUR_USER, userFacade.getToken(u), 0);
        }
    }


    public void saveValidateCodeAttribute(String code) {
        HttpServletUtil.getHttpRequest().getSession().setAttribute("validateCode", code);
    }

    public String getValidateCodeAttribute() {
        return HttpServletUtil.getHttpRequest().getSession().getAttribute("validateCode").toString();
    }
}
